package org.dromara.business.controller;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.ibatis.annotations.Param;
import org.dromara.business.domain.LzshBusinessInfo;
import org.dromara.business.domain.LzshBusinessInfoDataTotal;
import org.dromara.business.domain.LzshBusinessMenu;
import org.dromara.business.domain.LzshBusinessMenuNum;
import org.dromara.business.domain.bo.LzshBusinessMenuBo;
import org.dromara.business.domain.vo.LzshBusinessInfoVo;
import org.dromara.business.domain.vo.LzshBusinessMenuVo;
import org.dromara.business.domain.vo.LzshTemplateVo;
import org.dromara.business.service.ILzshTemplateService;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

import org.dromara.business.service.ILzshBusinessMenuService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商机填报系统菜单
 *
 * @author Lion Li
 * @date 2024-08-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/businessMenu")
public class LzshBusinessMenuController extends BaseController {

    private final ILzshBusinessMenuService lzshBusinessMenuService;
    private final ISysOssService ossService;
    private final ILzshTemplateService lzshTemplateService;

    /**
     * 查询商机填报系统菜单列表
     */
    @SaCheckPermission("business:businessMenu:list")
    @GetMapping("/list")
    public TableDataInfo<LzshBusinessMenuVo> list(LzshBusinessMenuBo bo, PageQuery pageQuery) {
        return lzshBusinessMenuService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商机填报系统菜单列表
     */
    @SaCheckPermission("business:businessMenu:export")
    @Log(title = "商机填报系统菜单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LzshBusinessMenuBo bo, HttpServletResponse response) {
        List<LzshBusinessMenuVo> list = lzshBusinessMenuService.queryList(bo);
        ExcelUtil.exportExcel(list, "商机填报系统菜单", LzshBusinessMenuVo.class, response);
    }

    /**
     * 获取商机填报系统菜单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:businessMenu:query")
    @GetMapping("/{id}")
    public R<LzshBusinessMenuVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lzshBusinessMenuService.queryById(id));
    }

    /**
     * 通过id获取模板
     *
     * @param id 主键
     */
    @GetMapping("/api/{id}")
    public R<LzshTemplateVo> getID(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        LzshBusinessMenuVo lzshBusinessMenuVo = lzshBusinessMenuService.queryById(id);
        if (lzshBusinessMenuVo != null )
        {
            Long templateId = lzshBusinessMenuVo.getTemplateId();
            return R.ok(lzshTemplateService.queryById(templateId));
        }
        else
            return R.ok(null);
    }

    @Autowired
    private ILzshBusinessMenuService iLzshBusinessMenuService;
    /**
     * 获取菜单的填报数量
     */
    @GetMapping("/api/menuNum")
    public R<LzshBusinessMenuNum>getMenuNum(){
        return R.ok(iLzshBusinessMenuService.getMenuNum());
    }


    /**
     * 获取菜单所有信息
     *
     *
     */
    @GetMapping("/api/list")
    public R<List<LzshBusinessMenuVo>> getAll()
    {
        LzshBusinessMenuBo bo = new LzshBusinessMenuBo();
        bo.setStatus("0");
        return R.ok(lzshBusinessMenuService.queryList(bo));
    }

    /**
     * 新增商机填报系统菜单
     */
    @SaCheckPermission("business:businessMenu:add")
    @Log(title = "商机填报系统菜单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LzshBusinessMenuBo bo) {
        SysOssVo oss = ossService.getById(Long.valueOf(bo.getImgUrl()));
        bo.setImgUrl(oss.getUrl());
        return toAjax(lzshBusinessMenuService.insertByBo(bo));
    }

    /**
     * 修改商机填报系统菜单
     */
    @SaCheckPermission("business:businessMenu:edit")
    @Log(title = "商机填报系统菜单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LzshBusinessMenuBo bo) {
        return toAjax(lzshBusinessMenuService.updateByBo(bo));
    }

    /**
     * 删除商机填报系统菜单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:businessMenu:remove")
    @Log(title = "商机填报系统菜单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lzshBusinessMenuService.deleteWithValidByIds(List.of(ids), true));
    }
}
